package com.cqc.jdk8.Jlang.object;

/**
 *
 */
public class ObjectWaitTest {

    /**
     * wait()会释放锁
     * 运行结果：
     * Thread-0...2025-6-26 22:08:06
     * Thread-1...2025-6-26 22:08:06
     * Object.wait()运行后会释放锁，所以下一个线程获取到了锁，并执行到 wait()，也进入等待状态，就卡住了。
     */
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();

        AThread aThread1 = new AThread(lock);
        AThread aThread2 = new AThread(lock);

        aThread1.start();
        aThread2.start();
    }

    /**
     * sleep(.) 不会释放锁，所以当前线程必须执行完，下一线程才会获取到锁并执行代码。、
     * 所以日志是先打印Thread-0，方法走完后，才是 Thread-1
     * 运行结果：
     * Thread-0...2025-6-26 22:10:54
     * Thread-0...2025-6-26 22:10:54
     * Thread-1...2025-6-26 22:10:54
     * Thread-1...2025-6-26 22:10:54
     */
    public static void main2(String[] args) throws InterruptedException {
        Object lock = new Object();

        BThread aThread1 = new BThread(lock);
        BThread aThread2 = new BThread(lock);

        aThread1.start();
        aThread2.start();
    }

}
